﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using Microsoft.Win32;

using Aulac.Global.Enums; using Aulac.Global.Data; using Aulac.Global.Common; using Aulac.Global.Xml;
using System.Collections.Generic;
using Aulac.Rsc.Ems; using Aulac.Rsc.Ems.Tags;
using Aulac.Srv.Db.Provider; using Aulac.Srv.Db.Connect;
using Aulac.Srv.Db.Data.Ems.InvJrns;
using Aulac.Srv.Db.Common;
using Aulac.Global.Exceptions;
using Aulac.Rsc;
using Aulac.Srv.Db.Data.Ems.AcntJrns;

namespace Aulac.Srv.Db.Data.Ems.Stores.TranStores
{
    public partial class VchTranStoreMng : Aulac.Srv.Db.Data.Ems.Document.DbEmsDataDocumentMangager, IVchTranStoreMng, Aulac.Srv.Db.Data.Ems.Interface.IVchSyncJrn
	{

		public VchTranStoreMng(IDbAulacConnect p_Db):base(p_Db)
		{
        }
         
        #region Override Methods
        
        protected override bool OnAddVoucher( ref XmlDataDocument xmlDoc)
        {
            string sVchID = "";
            XmlElement eVch = XmlProcess.GetVchElm(xmlDoc);
            AddVchHeader(eVch, ref sVchID);
            XmlNodeList list = XmlProcess.GetListVchLineItem(eVch);
            AddVchLines(list, sVchID);
            return SetInvJrn(xmlDoc, sVchID);
        }
        
        protected override bool OnDelVoucher( ref XmlDataDocument xmlDoc)
        {
            string sVoucherID = XmlProcess.GetValueParam(xmlDoc, tagVch.Inv.VchTranInvPrkID);
            string sVoucherType = XmlProcess.GetValueParam(xmlDoc, tagVch.Inv.VchTypeID);
            bool isCheckStoreID = XmlProcess.GetValueParamBoolean(xmlDoc, tagVch.Inv.IsCheckQtyLost);
            DelVchTranStore(sVoucherType, sVoucherID);
           
            InvJrnMng ivnJrn = new InvJrnMng(DbDataProvider.DbAulacConnection);
            XmlDataDocument xmlCheck = null;
            ivnJrn.DelIvnJrn(sVoucherID, sVoucherType, isCheckStoreID, out xmlCheck); 
            if(!XmlProcess.IsEmpty(xmlCheck))
                throw new AulacException(Aulac.ErrCodes.ErrorCode.Error0010, xmlCheck);
            return true;
        }
        
        protected override bool OnUpdateVoucher( ref XmlDataDocument xmlDoc)
        {
            /* Author Write: Thang */
            /* Code Update: 01/04/2010 by H.Rin */

            XmlElement eHeader = XmlProcess.GetVchHeaderElm(xmlDoc);
            string sVoucherID = XmlProcess.GetValueAtrb(eHeader, tagVch.Inv.VchTranInvPrkID);
            string sVoucherType  = XmlProcess.GetValueAtrb(eHeader, tagVch.Inv.VchTypeID);

            sqlQuery = SqlHelper.CreateSqlUpdate(TableNames.VchTranStoreHeader, VchTranInfo.ListH.GetRange(1, VchTranInfo.ListH.Count - 1).ToArray());
            sqlQuery += string.Format(" WHERE {0} = ? ", tagVch.Inv.VchTranInvPrkID);
            
            DbDataProvider.AddParamToCmd(mDbCmd, eHeader, VchTranInfo.ListH.GetRange(1, VchTranInfo.ListH.Count - 1).ToArray());
            DbDataProvider.AddParamToCmd(mDbCmd, false, sVoucherID);
            DbDataProvider.ExecuteNonQuery(ref mDbCmd, sqlQuery);

            DelVchLineDb(sVoucherID);
            XmlNodeList mNodeList = XmlProcess.GetListVchLineItem(xmlDoc);
            AddVchLines(mNodeList, sVoucherID);
            return SetInvJrn(xmlDoc, sVoucherID);
        }        
        protected override XmlDataDocument OnGetVoucher(XmlDataDocument xmlParam)
        {
            /* Date code: 01/03/2011 by Thangnc */
            return GetVchTranStoreDb(xmlParam);
        }
        protected override void OnGetListVoucher(ref DataTable pDataReturn, XmlDataDocument xmlPram)
        {
            try
            {
                pDataReturn = GetListVchTranStroreDb(xmlPram);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }

        }
        #endregion
 
        #region IVchInStock Members
        public void SyncPrimeCostForVch(string pOrgVchID, string pVchTypeID)
        {
            IInvJrnMng invMng = new InvJrnMng(DbDataProvider.DbAulacConnection);
            DataTable tbPriCost = invMng.GetPrimeCostOfVch(pOrgVchID, pVchTypeID);
            DataTable tbVch = GetVchTable(pOrgVchID);
            foreach (DataRow rowVch in tbVch.Rows)
            {
                sqlQuery = SqlHelper.CreateSqlUpdate(TableNames.VchTranStoreLine, tagVch.Inv.PrimeCostInExc, tagVch.Inv.AmtPrimeCostLineInExc);
                sqlQuery += string.Format(" WHERE ({0} = ?) AND ({1} = ?)", tagVch.Inv.VchTranInvPrkID, tagVch.Inv.ItemLine);

                DataRow rowPrimeCost = tbPriCost.Select(string.Format("{0} = {1} AND {2} = {3}",
                    tagVch.Inv.StorePrkID, Aulac.Global.Data.DataTableProcess.GetValue(rowVch, tagVch.Inv.StoreRelsPrkID),
                    tagVch.Inv.ItemPrkID, Aulac.Global.Data.DataTableProcess.GetValue(rowVch, tagVch.Inv.ItemPrkID)))[0];

                double dPrimeCost = Aulac.Global.Data.DataTableProcess.GetValueNumber(rowPrimeCost, tagVch.Inv.PrimeCostInExc);
                DbDataProvider.AddParamToCmd(mDbCmd, dPrimeCost,
                    Aulac.Global.Data.DataTableProcess.GetValueNumber(rowVch, tagVch.Inv.Quantity) * dPrimeCost,
                    pOrgVchID,
                    Aulac.Global.Data.DataTableProcess.GetValue(rowVch, tagVch.Inv.ItemLine));
                DbDataProvider.ExecuteNonQuery(ref mDbCmd, sqlQuery);
            }
        }

        #endregion

        #region IVchTranStoreMng Members
        public XmlDataDocument GetNewVchNo(XmlDataDocument xmlParam)
        {
            /* Code date: 12/17/2010 by Thangnc*/

            string sVchTypeID = XmlProcess.GetValueParam(xmlParam, tagVch.Inv.VchTypeID);
            XmlDataDocument xmlOutDoc = CreateXmlDocument("", "");
            string sVchName = "";
            string sVchNo = GetNewVchNoDb(sVchTypeID, ref sVchName);
            XmlProcess.SetAtrbVchHeader(xmlOutDoc, tagVch.Inv.VchNo, sVchNo);
            XmlProcess.SetAtrbVchHeader(xmlOutDoc, tagVch.Inv.VchName, sVchName);
            return xmlOutDoc;
        }

        #endregion
    }
}
